############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A1: Candidate Emergence, by District Composition and Racial/Ethnic Group
############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

# Change candidate variable to a binary, 0 = no candidate, 1 = one or more candidates
sl_data$white_cand[sl_data$white_cand > 1] <- 1
sl_data$black_cand[sl_data$black_cand > 1] <- 1
sl_data$latino_cand[sl_data$latino_cand > 1] <- 1
sl_data$asian_cand[sl_data$asian_cand > 1] <- 1

## FigA1a, Upper Left Panel of Figure A1: Whites
figA1a <- ggplot(data=sl_data, aes(x=white_pct, y=white_cand))
figA1a <- figA1a + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA1a <- figA1a + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA1a <- figA1a + geom_point(alpha=0.2)
figA1a <- figA1a + ggtitle("Whites")
figA1a <- figA1a + scale_x_continuous("Percent White in State Legislative District")
figA1a <- figA1a + scale_y_continuous("Probability of White Candidate", limits=c(-0.05,1.05))
figA1a <- figA1a + coord_cartesian(ylim=c(0,1))
figA1a <- figA1a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA1a

## FigA1b, Upper Left Panel of Figure A1: African-Americans
figA1b <- ggplot(data=sl_data, aes(x=black_pct, y=black_cand))
figA1b <- figA1b + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA1b <- figA1b + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA1b <- figA1b + geom_point(alpha=0.2)
figA1b <- figA1b + ggtitle("African-Americans")
figA1b <- figA1b + scale_x_continuous("Percent Black in State Legislative District")
figA1b <- figA1b + scale_y_continuous("Probability of Black Candidate", limits=c(-0.05,1.05))
figA1b <- figA1b + coord_cartesian(ylim=c(0,1))
figA1b <- figA1b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA1b

## FigA1c, Upper Left Panel of Figure A1: Latinos
figA1c <- ggplot(data=sl_data, aes(x=latino_pct, y=latino_cand))
figA1c <- figA1c + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA1c <- figA1c + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA1c <- figA1c + geom_point(alpha=0.2)
figA1c <- figA1c + ggtitle("Latinos")
figA1c <- figA1c + scale_x_continuous("Percent Latino in State Legislative District")
figA1c <- figA1c + scale_y_continuous("Probability of Latino Candidate", limits=c(-0.05,1.05))
figA1c <- figA1c + coord_cartesian(ylim=c(0,1))
figA1c <- figA1c + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA1c

## FigA1d, Upper Left Panel of Figure A1: Asians
figA1d <- ggplot(data=sl_data, aes(x=asian_pct, y=asian_cand))
figA1d <- figA1d + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA1d <- figA1d + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA1d <- figA1d + geom_point(alpha=0.2)
figA1d <- figA1d + ggtitle("Asians")
figA1d <- figA1d + scale_x_continuous("Percent Asian in State Legislative District")
figA1d <- figA1d + scale_y_continuous("Probability of Asian Candidate", limits=c(-0.05,1.05))
figA1d <- figA1d + coord_cartesian(ylim=c(0,1))
figA1d <- figA1d + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA1d